﻿var controller = this.lookupController();
var status = controller.analysis().status;
var expenseType = ref.ExpenseType.getValue();

controller.balance = controller.balance || {};
controller.budgetControl = controller.budgetControl || {};

var budgetControl = "N";
var store2 = ref.datagrid2.getStore();
var store7 = ref.datagrid7.getStore();

var grid4 = ref.datagrid4,
    editor4 = grid4.getColumns()[0].getEditor && grid4.getColumns()[0].getEditor();

if ((status == 'launch' || status == 'resubmit') && editor4) {
    var record = store.getUpdatedRecords()[0],
        flag = 'update';

    if (!record) {
        record = store.getNewRecords()[0];
        flag = 'add';
    }

    if (!record) {
        flag = 'reset';
    }

    if (record) {
        //时时抓取Balance数据

        controller.getBalance(record.get("ProjectActivity"));

        if (flag == 'update') {
            if (!Ext.Object.isEmpty(record.modified)) {
                var companySharingRatio = record.get("CompanySharingRatio");

                if (companySharingRatio) {
                    //CompanySharingRatio 不能输入小于0 或大于100的数 否则清空.
                    if (parseFloat(companySharingRatio) < 0 || parseFloat(companySharingRatio) > 100) {
                        record.data.CompanySharingRatio = record.previousValues ? record.previousValues.CompanySharingRatio : 0;
                    }
                }

                var purStore = expenseType == "4" ? store7 : store2;
                var r = purStore.findRecord('DescriptionOfSuppliesServices', record.data.DescriptionOfSuppliesServices);
                if (r) {
                    record.data.ProjectActivity = r.data.ProjectActivity;
                    record.data.Cost = (r.data.TotalPrice * record.data.CompanySharingRatio) / 100;
                }

                if (record.modified.DescriptionOfSuppliesServices !== undefined) {

                    if (record.data.Company === 'JLRC') {
                        var r = editor4.getStore().findRecord('code', record.modified.DescriptionOfSuppliesServices);

                        if (r) {
                            r.data.name = record.data.DescriptionOfSuppliesServices;
                            r.data.code = record.data.DescriptionOfSuppliesServices;

                            var store = grid4.getStore();
                            for (var i = 0; i < store.data.items.length; i++) {
                                var item = store.data.items[i].data;
                                if (item.DescriptionOfSuppliesServices == record.modified.DescriptionOfSuppliesServices) {
                                    item.DescriptionOfSuppliesServices = record.data.DescriptionOfSuppliesServices;
                                    item.ProjectActivity = record.data.ProjectActivity;
                                    store.data.items[i].commit();
                                }
                            }
                        }
                    }
                }

                if (record.modified.Company !== undefined) {
                    // 控制Brand表格的显示和影藏
                    var hasJLRC = false;
                    var hasLRE_100Ratio = false;
                    for (var i = 0; i < store.data.items.length; i++) {
                        if (store.data.items[i].data.Company == 'JLRC') {
                            hasJLRC = true;
                        }
                    }

                    ref.section39.setHidden(!hasJLRC);

                    // 配置Brand表格中DescriptionOfSuppliesServices的下拉框
                    if (record.data.DescriptionOfSuppliesServices !== '') {
                        if (record.modified.Company === 'JLRC') {
                            var r = editor4.getStore().findRecord('code', record.data.DescriptionOfSuppliesServices);
                            editor4.getStore().remove(r);

                            var store4 = ref.datagrid4.getStore();
                            var rr = store4.findRecord('DescriptionOfSuppliesServices', record.data.DescriptionOfSuppliesServices);
                            store4.remove(rr);
                        }
                    }
                }

                if (record.modified.DescriptionOfSuppliesServices !== undefined || record.modified.CompanySharingRatio !== undefined || record.modified.Company !== undefined) {
                    var store = expenseType == "4" ? store7 : store2;

                    var r = store.findRecord('DescriptionOfSuppliesServices', record.data.DescriptionOfSuppliesServices);
                    if (r) {
                        record.data.Cost = (r.data.TotalPrice * record.data.CompanySharingRatio) / 100;
                    }
                }

                if (record.data.Company === 'JLRC') {
                    var store4 = grid4.getStore();
                    for (var i = 0; i < store4.data.items.length; i++) {
                        var item = store4.data.items[i];
                        if (item.get('DescriptionOfSuppliesServices') == record.data.DescriptionOfSuppliesServices) {

                            if (item.data.JASharingRatio !== undefined) {
                                var companyCost = record.data.Cost;
                                item.data.JACost = parseFloat(((companyCost * item.data.JASharingRatio) / 100).toFixed(2));
                                item.data.LRCost = companyCost - item.data.JACost;
                                item.data.ProjectActivity = record.data.ProjectActivity;
                                item.commit();
                            }
                        }
                    }
                }
            }
            record.commit();
        } else if (flag == 'add') {
            var gridRecordList = ref.datagrid2.getStore().data.items;

            if (ref.ExpenseType.getValue() == '6' && gridRecordList.length == 1) {
                setTimeout(function () {
                    record.set('DescriptionOfSuppliesServices', gridRecordList[0].get('DescriptionOfSuppliesServices'));
                }, 200);
            }

            record.commit();
        }
    }

    //数据变更 需要同步Brand Allocation（JLRC）
    var editor4 = ref.datagrid4.getColumns()[0].getEditor();
    var obj = [];
    editor4.getStore().removeAll();

    Ext.each(ref.datagrid3.store.data.items, function (record3) {
        var isObjExists = obj.filter(function (o) {
            return o.name == record3.get('DescriptionOfSuppliesServices');
        });
        if (isObjExists.length == 0) {
            if (record3.get('Company') == 'JLRC') {
                obj.push({
                    name: record3.get('DescriptionOfSuppliesServices'),
                    code: record3.get('DescriptionOfSuppliesServices')
                });
            }
        }
    });

    editor4.getStore().loadData(obj);
}

//数据变更 需要同步Total
var amoutObj = {
    JLRC: 0,
    CJLR: 0,
    LRE: 0
};

Ext.each(ref.datagrid3.store.data.items, function (record) {
    var company = record.get('Company');

    if (record.get('Cost')) {
        amoutObj[company] = parseFloat((amoutObj[company] + record.get('Cost')).toFixed(2));
    }
});
var layoutColumns = 5;
for (var c in amoutObj) {
    if (c) {
        if (amoutObj[c]) {
            ref['total' + c].show();
            ref['total' + c].setValue(Ext.util.Format.number(amoutObj[c], '0,000.00'));
        } else {
            layoutColumns--;
            ref['total' + c].hide();
        }
    }
}
ref.section44.layoutColumns = layoutColumns;